package org.cloudnote.controller;

import org.cloudnote.bean.NoteBook;
import org.cloudnote.bean.NoteResult;
import org.cloudnote.service.NoteBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/notebook")
public class NoteBookController {

    @Autowired
    private NoteBookService noteBookService;

    @RequestMapping("/loadbooks.do")
    public NoteResult getByUserId(String userId){
        List<NoteBook> noteBooks = noteBookService.getByUserId(userId);
        NoteResult result = new NoteResult();
        result.setStatus(0);
        result.setMsg("查询成功");
        result.setData(noteBooks);
        return result;
    }
    @RequestMapping("/save.do")
    public NoteResult save(NoteBook noteBook){
        NoteResult result = new NoteResult();
        if(noteBook.getNoteBookName()==null||noteBook.getNoteBookName()==""){
            result.setStatus(1);
            result.setMsg("名字不能为空");
            return result;
        }
        if(noteBook.getUserId()==null){
            result.setStatus(1);
            result.setMsg("创建者不能为空");
            return result;
        }
        int count = noteBookService.save(noteBook);
        if(count==0){
            result.setStatus(1);
            result.setMsg("添加失败");
            return result;
        }
        result.setStatus(0);
        result.setData(noteBook);
        result.setMsg("添加成功");
        return  result;
    }

}
